import React, { Component } from 'react';
import ReactDOM from 'react-dom';
import { connect } from 'react-redux';
import { Table, Row, Col, Button } from 'antd';
import { hashHistory } from 'react-router';
import _ from 'lodash';
import {
	
} from '../../action';

class SearchList extends Component {
	constructor(props) {
		super(props);
		this.columns = [
			{ title: '姓名', dataIndex: 'name', key: 'name' },
			{ title: '性别', dataIndex: 'sex', key: 'sex' },
			{ title: '身份证号', dataIndex: 'id', key: 'id', className: 'center-col' }
		];
		this.state = {}
	}
	render() {
		const { nodes } = this.props;
		let nodeArray = [];
		const nodeArr = Object.keys(nodes || []).reduce((pre,key)=> {
			if(nodeArray.indexOf(nodes[key].labels[0])==-1) {
				nodeArray.push(nodes[key].labels[0]);
			}
			return nodeArray;
		}, [])
		const nodeRender = nodeArr.reduce((pre, item) => {
			const innerObj = nodes.reduce((preInner, itemInner, index) => {
				if (item === itemInner.labels[0]) {
					preInner.label = item;
					preInner.value = [...(preInner.value || []), itemInner.properties];
					preInner.columns = Object.keys(itemInner.properties)
					return preInner;
				} else {
					return preInner;
				}
			}, {});
			return [...pre, innerObj]
		}, []);
		return (
			<div className="search-list">
				{
					nodeRender.map((n,i)=> {
						const columns = n.columns.map(c=> {
							return Object.assign(c, {title: c, dataIndex: c, key: c});
						});
						const dataSource = n.value.map((v,i)=> {
							return Object.assign(v, {key: i});
						});
						return (
						<Row type="flex" justify="space-around" align="middle" key={i} style={{marginBottom: 20}}>
							<Col span={2} style={{textAlign: 'center'}}>
								<span>{n.label}</span>
							</Col>
							<Col span={20} offset={2}>
								<Table columns={columns} dataSource={dataSource} pagination={false}/>
							</Col>
						</Row>
						)
					})
				}
			</div>
		)
	}
}

function mapState2Props(state) {
	let { graphData: {relationships, nodes} } = state;
	return { relationships, nodes };
}

SearchList = connect(mapState2Props)(SearchList);

export default SearchList;
